#include <stdio.h>
#include <stdlib.h>
#include "md5.h"

#define BUF_SIZE 1024

char buf[BUF_SIZE];

md5_byte_t* getFileMD5(FILE * fp, int len, md5_byte_t* digest){

	md5_state_t state;

	md5_init(&state);
	rewind(fp);
	while(len > BUF_SIZE){
		fread(buf, BUF_SIZE, 1, fp);
		md5_append(&state, (const md5_byte_t *)buf, BUF_SIZE);
		len -= BUF_SIZE;
	}
	if(len != 0){
		fread(buf, len, 1, fp);
		md5_append(&state, (const md5_byte_t *)buf, len);
	}

	md5_finish(&state, digest);

	return digest;
}


char* md5ByteToString(md5_byte_t * digest){
	char * md5 = new char[32];
	for (int di = 0; di < 16; ++di)
	{
		sprintf(md5+di*2,"%02x",digest[di]);
	}      
	return md5;
}
